<template>
  <div>
    <!-- 新增任务 -->
    <el-dialog
      v-model="dialogVisible"
      title="新增任务"
      width="300"
      :before-close="cancel"
      :append-to-body="true"
    >
      <span>
        <!-- 表单 -->
        <el-form ref="ruleFormRef" :model="serviceData" label-width="auto">
          <el-form-item label="服务项目：">
            <el-select v-model="serviceData.serviceId" placeholder="请选择服务项目">
              <el-option
                v-for="item in serviceList"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="开始时间：">
            <el-time-picker
              v-model="serviceData.startTime"
              placeholder="选择开始时间"
              format="HH:mm"
              value-format="HH:mm"
            />
          </el-form-item>
          <el-form-item label="结束时间：">
            <el-time-picker
              v-model="serviceData.endTime"
              placeholder="选择结束时间"
              format="HH:mm"
              value-format="HH:mm"
              @change="changeTime"
            />
          </el-form-item>
        </el-form>
      </span>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="cancel">取消</el-button>
          <el-button type="primary" @click="sure">确认</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted, defineEmits, defineProps } from "vue";
import { nursingList } from "@/api/config/nursingService/nursingService";

const emit = defineEmits(["close"]);
const props = defineProps(["startTime"]);

const serviceData = reactive({
  serviceId: undefined,
  startTime: props.startTime < 10 ? "0" + props.startTime + ":00" : props.startTime + ":00",
  endTime: "",
});

//dialog默认显示
const dialogVisible = ref(true);

//服务列表
const serviceList = ref([]);

const getServiceData = async () => {
  let res = await nursingList();
  serviceList.value = res.list;
};

//选择时间
const changeTime = (time) => {
  if (time < serviceData.startTime) {
    ElMessage.error("当前时间不得小于开始时间");
    serviceData.endTime = "";
  }
};

//取消
const cancel = () => {
  emit("close");
};
//确定
const sure = () => {
  emit("close", serviceData);
};

onMounted(() => {
  getServiceData();
});
</script>

<style lang="scss" scoped></style>
